//
//  HRGiftMessageCell.m
//  SpeedDating
//
//  Created by ISU1 on 17/2/10.
//  Copyright © 2017年 isu. All rights reserved.
//

#import "HRGiftMessageCell.h"

#define Test_Message_Font_Size 16

#define MaxWidth [UIScreen mainScreen].bounds.size.width-(10+[RCIM sharedRCIM].globalMessagePortraitSize.width+10)*2-5-35

@implementation HRGiftMessageCell

+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    HRGiftMessage *message = (HRGiftMessage *)model.content;
    CGSize size = [HRGiftMessageCell getBubbleBackgroundViewSize:message];
    
    CGFloat __messagecontentview_height = size.height;
    __messagecontentview_height += extraHeight;
    
    return CGSizeMake(collectionViewWidth, __messagecontentview_height);
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    self.bubbleBackgroundView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.messageContentView addSubview:self.bubbleBackgroundView];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.textLabel = [[RCAttributedLabel alloc] initWithFrame:CGRectZero];
    [self.textLabel setFont:[UIFont systemFontOfSize:Test_Message_Font_Size]];
    self.textLabel.numberOfLines = 0;
    [self.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [self.textLabel setTextAlignment:NSTextAlignmentLeft];
    [self.textLabel setTextColor:[UIColor blackColor]];
    [self.bubbleBackgroundView addSubview:self.imageView];
    [self.bubbleBackgroundView addSubview:self.textLabel];
    UILongPressGestureRecognizer *longPress =
    [[UILongPressGestureRecognizer alloc]
     initWithTarget:self
     action:@selector(longPressed:)];
    [self.bubbleBackgroundView addGestureRecognizer:longPress];
    
    UITapGestureRecognizer *textMessageTap = [[UITapGestureRecognizer alloc]
                                              initWithTarget:self
                                              action:@selector(tapTextMessage:)];
    textMessageTap.numberOfTapsRequired = 1;
    [self.bubbleBackgroundView addGestureRecognizer:textMessageTap];
    self.bubbleBackgroundView.userInteractionEnabled  =YES;
}

- (void)setDataModel:(RCMessageModel *)model {
    [super setDataModel:model];
    [self setAutoLayout];
}

- (void)setAutoLayout {
    NSArray *contentArray = @[];
    HRGiftMessage *testMessage = (HRGiftMessage *)self.model.content;
    if (testMessage) {
        contentArray = [testMessage.content componentsSeparatedByString:@","];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(contentArray[2])] placeholderImage:[UIImage imageNamed:@"llustration"]];
    }
    CGSize imageViewSize = [[self class] getImageViewSize:testMessage];
    CGSize textLabelSize = [[self class] getTextLabelSize:testMessage];
    CGSize bubbleBackgroundViewSize = [[self class] getBubbleSize:textLabelSize imageViewSize:imageViewSize];
    CGRect messageContentViewRect = self.messageContentView.frame;
    if (MessageDirection_RECEIVE == self.messageDirection) {
        self.textLabel.text = contentArray[1];
        self.textLabel.frame =
        CGRectMake(20, 10+5+imageViewSize.height, textLabelSize.width, textLabelSize.height);
        self.imageView.frame = CGRectMake(20, 10, imageViewSize.width, imageViewSize.height);
        if (imageViewSize.width<textLabelSize.width) {
            self.imageView.frame = CGRectMake(20+(textLabelSize.width-imageViewSize.width)/2.0, 10, imageViewSize.width, imageViewSize.height);
        }
        messageContentViewRect.size.width = bubbleBackgroundViewSize.width;
        self.messageContentView.frame = messageContentViewRect;
        
        self.bubbleBackgroundView.frame = CGRectMake(
                                                     0, 0, bubbleBackgroundViewSize.width, bubbleBackgroundViewSize.height);
        UIImage *image = [RCKitUtility imageNamed:@"chat_from_bg_normal"
                                         ofBundle:@"RongCloud.bundle"];
        self.bubbleBackgroundView.image = [image
                                           resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.8,
                                                                                        image.size.width * 0.8,
                                                                                        image.size.height * 0.2,
                                                                                        image.size.width * 0.2)];
    } else {
        self.textLabel.text = contentArray[0];
        self.imageView.frame = CGRectMake(12, 10, imageViewSize.width, imageViewSize.height);
        if (imageViewSize.width<textLabelSize.width) {
            self.imageView.frame = CGRectMake(12+(textLabelSize.width-imageViewSize.width)/2.0, 10, imageViewSize.width, imageViewSize.height);
        }
        self.textLabel.frame =
        CGRectMake(12, 10+5+imageViewSize.height, textLabelSize.width, textLabelSize.height);
        messageContentViewRect.size.width = bubbleBackgroundViewSize.width;
        messageContentViewRect.size.height = bubbleBackgroundViewSize.height;
        messageContentViewRect.origin.x =
        self.baseContentView.bounds.size.width -
        (messageContentViewRect.size.width + HeadAndContentSpacing +
         [RCIM sharedRCIM].globalMessagePortraitSize.width + 10);
        self.messageContentView.frame = messageContentViewRect;
        
        self.bubbleBackgroundView.frame = CGRectMake(
                                                     0, 0, bubbleBackgroundViewSize.width, bubbleBackgroundViewSize.height);
        UIImage *image = [RCKitUtility imageNamed:@"chat_to_bg_normal"
                                         ofBundle:@"RongCloud.bundle"];
        self.bubbleBackgroundView.image = [image
                                           resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.8,
                                                                                        image.size.width * 0.2,
                                                                                        image.size.height * 0.2,
                                                                                        image.size.width * 0.8)];
    }
}

+ (CGSize)getImageViewSize:(HRGiftMessage *)message {
    if (message.content.length) {
        return CGSizeMake(KScreenWidth/4.0, KScreenWidth/4.0+5);
    } else {
        return CGSizeZero;
    }
}

+ (CGSize)getTextLabelSize:(HRGiftMessage *)message {
    if (message.content.length) {
        NSString *text = @"";
        NSArray *array = [message.content componentsSeparatedByString:@","];
        if ([array[3] isEqualToString:[HQHttpManager getUserId]]) {
            text = array[0];
        } else {
            text = array[1];
        }
        float maxWidth =
        [UIScreen mainScreen].bounds.size.width -
        (10 + [RCIM sharedRCIM].globalMessagePortraitSize.width + 10) * 2 - 5 -
        35;
        CGRect textRect = [text
                           boundingRectWithSize:CGSizeMake(maxWidth, 8000)
                           options:(NSStringDrawingTruncatesLastVisibleLine |
                                    NSStringDrawingUsesLineFragmentOrigin |
                                    NSStringDrawingUsesFontLeading)
                           attributes:@{
                                        NSFontAttributeName :
                                            [UIFont systemFontOfSize:Test_Message_Font_Size]
                                        }
                           context:nil];
        textRect.size.height = ceilf(textRect.size.height);
        textRect.size.width = ceilf(textRect.size.width);
        return CGSizeMake(textRect.size.width + 5, textRect.size.height + 5);
    } else {
        return CGSizeZero;
    }
}

+ (CGSize)getBubbleSize:(CGSize)textLabelSize imageViewSize:(CGSize)imageViewSize {
    CGSize bubbleSize = CGSizeZero;
    if (imageViewSize.width > textLabelSize.width) {
        bubbleSize.width = imageViewSize.width;
    } else {
        bubbleSize.width = textLabelSize.width;
    }
    if (imageViewSize.width > 0) {
        bubbleSize.height = textLabelSize.height + imageViewSize.height + 10;
    } else {
        bubbleSize.height = textLabelSize.height + 10;
    }
    bubbleSize.width = bubbleSize.width + 12 + 20;
    bubbleSize.height = bubbleSize.height + 7 + 7;
    return bubbleSize;
}

+ (CGSize)getBubbleBackgroundViewSize:(HRGiftMessage *)message {
    CGSize imageViewSize = [[self class] getImageViewSize:message];
    CGSize textLabelSize = [[self class] getTextLabelSize:message];
    return [[self class] getBubbleSize:textLabelSize imageViewSize:imageViewSize];
}

- (void)longPressed:(id)sender {
    UILongPressGestureRecognizer *press = (UILongPressGestureRecognizer *)sender;
    if (press.state == UIGestureRecognizerStateEnded) {
        return;
    } else if (press.state == UIGestureRecognizerStateBegan) {
        [self.delegate didLongTouchMessageCell:self.model
                                        inView:self.bubbleBackgroundView];
    }
}

- (void)tapTextMessage:(UIGestureRecognizer *)gestureRecognizer {
    HRGiftMessage *message = (HRGiftMessage *)self.model.content;
    RCMessageModel *model = [RCMessageModel new];
    model.objectName = @"app:GiftMessage";
    model.content = message;
    if ([self.delegate respondsToSelector:@selector(didTapMessageCell:)]) {
        [self.delegate didTapMessageCell:model];
    }
}

@end
